package com.lg.distributed.transaction.message.config;

import com.lg.distributed.transaction.message.constant.MQConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 死信队列
 *
 * @author by Mr. Li 2020/12/12 15:54
 */
@Configuration
public class DeadQueue {

    @Bean
    public Exchange dlxExchange() {
        return new TopicExchange(MQConstants.DLX_EXCHANGE, true, false);
    }

    @Bean("delayQueue")
    public Queue delayQueue() {
        //Map<String, Object> arguments = new HashMap<>();
        //queue.addArgument();

        /**
         * 需要设置三个设置
         * x-dead-letter-exchange:order-event-exchange
         * x-dead-letter-routing-key:order.release.order
         * x-message-ttl:60000
         *
         * 给当前队列绑定一个交换机，设置一个路由键，设置一个过期时间
         *
         */
        //arguments.put("x-dead-letter-exchange", MQConstants.DLX_EXCHANGE);
        //arguments.put("x-dead-letter-routing-key", MQConstants.DLX_ROUTING_KEY);
        //
        //arguments.put("x-message-ttl", 60000);

        // 根据 业务 动态获取

        return new Queue(MQConstants.DLX_QUEUE, true, false, false);
    }

    /**
     * order.create.order 路由键
     *
     * @return
     */
    @Bean
    public Binding orderCreateOrderBinding() {
        // String destination, Binding.DestinationType destinationType, String exchange, String routingKey, @Nullable Map<String, Object> arguments) {
        // 目的queue ，绑定类型，交换机名称，路由键，参数
        return new Binding(MQConstants.DLX_QUEUE,
                Binding.DestinationType.QUEUE,
                MQConstants.DLX_EXCHANGE,
                MQConstants.DLX_ROUTING_KEY, null);
    }
}
